package com.ruoyi.back.controller

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.ruoyi.app.common.*
import com.ruoyi.back.domain.v4.ContentSourceCheck
import com.ruoyi.back.domain.v4.ContentSourceCheckSetting
import com.ruoyi.back.mapper.ContentSourceCheckMapper
import com.ruoyi.back.mapper.ContentSourceCheckSettingMapper
import com.ruoyi.common.core.page.TableSupport
import io.swagger.annotations.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/system/contentSourceCheck")
@Api(tags = ["视频 && 图片 自动审核"])
class ContentSourceCheckController {

    @Autowired
    private lateinit var contentSourceCheckMapper: ContentSourceCheckMapper

    @Autowired
    private lateinit var contentSourceCheckSettingMapper: ContentSourceCheckSettingMapper


    @ApiModel("视频图片自动审核查询参数")
    data class QueryParam(

        @ApiModelProperty("用户信息")
        var userInfo: String? = null,

        @ApiModelProperty("1. 视频 2.图片")
        var type: Int? = null,

        @ApiModelProperty("审核状态 2.审核通过 3.审核失败")
        var checkStatus: Int? = null,
    )

    @ApiModel("列表显示参数")
    data class ListParam(

        var id: Long? = null,

        @ApiModelProperty("审核状态 1.待审核 2.审核成功 3.审核失败")
        var checkStatus: Int? = null,

        @ApiModelProperty("来源-视频or图片")
        var source: String? = null,

        @ApiModelProperty("审核内容")
        var text: String? = null,

        @ApiModelProperty("用户id")
        var userId: String? = null,

        @ApiModelProperty("用户名")
        var username: String? = null,

        @ApiModelProperty("创建时间")
        var createTime: String? = null,
    )

    @ApiOperation("视频 && 图片 自动审核分页")
    @GetMapping("list")
    fun contentSourceCheckList(queryParam: QueryParam): PageData<ListParam> {

        return TableSupport.buildPageRequest().let { pageDomain ->

            contentSourceCheckMapper.selectPage(
                Page(pageDomain.pageNum.toLong(), pageDomain.pageSize.toLong()),
                KtQueryWrapper(ContentSourceCheck::class.java)
                    .eq(
                        StrUtil.isNotBlank(queryParam.userInfo),
                        ContentSourceCheck::username,
                        queryParam.userInfo
                    )
                    .or().eq(
                        StrUtil.isNotBlank(queryParam.userInfo),
                        ContentSourceCheck::userId,
                        queryParam.userInfo
                    )
                    .eq(queryParam.checkStatus != null, ContentSourceCheck::checkStatus, queryParam.checkStatus)
                    .eq(ContentSourceCheck::type, queryParam.type)
                    .orderByDesc(ContentSourceCheck::createTime)
            )
                .let { contentCheckPage ->
                    Page<ListParam>().apply {
                        BeanUtil.copyProperties(contentCheckPage, this)
                        records = contentCheckPage.records.takeIf { it.isNotEmpty() }
                            ?.run {
                                map {
                                    ListParam().apply {
                                        BeanUtil.copyProperties(it, this)
                                        createTime = it.createTime?.format()
                                    }
                                }
                            }
                    }.pageInfo()
                }
        }
    }


    @ApiOperation("移除审核信息")
    @ApiImplicitParams(
        ApiImplicitParam(name = "ids", value = "用户id 多个使用逗号隔开", type = "path", required = true)
    )
    @PostMapping("remove/{ids}")
    fun removeContentCheck(@PathVariable ids: String): Data<Boolean> =
        Ok("移除成功", contentSourceCheckMapper.deleteBatchIds(ids.split(",")) > 0)


    @ApiOperation("设置是否开启审核")
    @ApiImplicitParams(
        ApiImplicitParam(name = "open", value = "是否开启 是传true 不是传false", type = "path", required = true)
    )
    @PostMapping("setting")
    fun setting(@RequestBody contentSourceCheckSetting: ContentSourceCheckSetting): Data<Boolean> {

        contentSourceCheckSetting.id?.run { contentSourceCheckSettingMapper.updateById(contentSourceCheckSetting) }
            ?: run {
                contentSourceCheckSettingMapper.insert(ContentSourceCheckSetting().apply {
                    BeanUtil.copyProperties(contentSourceCheckSetting, this)
                })
            }

        return Ok("设置成功", true)
    }

    @ApiOperation("获取图片视频审核的开启状态")
    @ApiImplicitParams(
        ApiImplicitParam(name = "type", value = "1. 视频 2.图片", type = "path", required = true)
    )
    @GetMapping("remove/{type}")
    fun settingInfo(@PathVariable type: Int): Data<ContentSourceCheckSetting> {
        return Ok(
            "获取成功", contentSourceCheckSettingMapper.selectOne(
                KtQueryWrapper(ContentSourceCheckSetting::class.java)
                    .eq(ContentSourceCheckSetting::type, type)
            )
        )
    }
}